package com.immo.libcommon.view;


import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @content 底部导航的封装
 * @date 2017/11/10
 */

public class TabLayoutView extends LinearLayout {

    private Context context;
    private String[] titles; //要显示的标题
    private int[] imgs; //图标
    private int[] imgsSelected; //图标

    private int imgWidth;//图片宽度
    private int imgHeight; //图片高度
    private int txtSize; //标题大小
    private int txtColor; //标题未选中颜色
    private int txtSelectedColor; //选择颜色
    private List<TextView> textViews; //保存标题
    private List<MessageTagView> imageViews; //保存图片
    private int currentIndex = 0;

    private OnItemOnclickListener onItemOnclickListener;

    public TabLayoutView(Context context) {
        this(context, null);
    }

    public TabLayoutView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TabLayoutView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
    }

    public void setOnItemOnclickListener(OnItemOnclickListener onItemOnclickListener) {
        this.onItemOnclickListener = onItemOnclickListener;
    }

    /**
     * 设置标题、图片和当前选中的条目
     *
     * @param tabTxt tab的文字数组
     * @param imgs 未选中图片
     * @param imgsSelected  选中后的图片
     * @param currentIndex 当前选中的位置
     */
    public void setDataSource(String[]tabTxt, int[] imgs, int[] imgsSelected, int currentIndex) {
        this.titles = tabTxt;
        this.imgs = imgs;
        this.currentIndex = currentIndex;
        this.imgsSelected = imgsSelected;
    }

    /**
     * 设置图标大小
     *
     * @param imgWidth  图标的宽度
     * @param imgHeight 图标的高度
     */
    public void setImageStyle(int imgWidth, int imgHeight) {
        this.imgWidth = dip2px(context, imgWidth);
        this.imgHeight = dip2px(context, imgHeight);
    }

    /**
     * 设置标题颜色
     *
     * @param txtSize          文字大小
     * @param txtColor         文字未选中颜色
     * @param txtSelectedColor 文字选中时颜色
     */
    public void setTextStyle(int txtSize, int txtColor, int txtSelectedColor) {
        this.txtSize = txtSize;
        this.txtColor = txtColor;
        this.txtSelectedColor = txtSelectedColor;
    }

    /**
     * 动态布局
     * 1、外层为横向线下布局
     * 2、动态添加相对布局，平分父布局，使宽度一致，添加到横向布局中
     * 3、总线布局添加图标和标题，并添加到相对布局中
     * 4、添加圆点到相对布局中，并设置在3的右上角
     */
    public void initDatas() {
        textViews = new ArrayList<>();
        imageViews = new ArrayList<>();
        //设置最外层布局
        LayoutParams outParams = new LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT);
        outParams.weight = 1;
        //设置图片的布局样式
        LayoutParams imgParams = new LayoutParams(imgWidth*2, imgHeight);
        imgParams.gravity = Gravity.CENTER;
        //设置文字的布局样式
        LayoutParams txtParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        txtParams.gravity = Gravity.CENTER;

        int size = titles.length;
        for (int i = 0; i < size; i++) {
            //创建一个ImageView
            MessageTagView imageView = new MessageTagView(context);
            imageView.setLayoutParams(imgParams);
            imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
            //创建一个TextView
            TextView textView = new TextView(context);
            textView.setText(titles[i]);
            textView.setLayoutParams(txtParams);
            textView.setTextSize(txtSize);
            //创建item的外围布局
            LinearLayout itemParams = new LinearLayout(context);
            itemParams.setGravity(Gravity.CENTER);
            itemParams.setOrientation(VERTICAL);
            itemParams.addView(imageView);
            //底部item的点击事件
            final int index = i;
            itemParams.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    setSelectStyle(index);
                    if (onItemOnclickListener != null) {
                        onItemOnclickListener.onItemClick(index);
                    }
                }
            });
            itemParams.addView(textView);
            addView(itemParams, outParams);
            textViews.add(textView);
            imageViews.add(imageView);
        }
        setSelectStyle(currentIndex);
    }

    /**
     * 设置选中样式
     * @param index 选中的item
     */
    public void setSelectStyle(int index) {
        int size = titles.length;
        for (int i = 0; i < size; i++) {
            textViews.get(i).setTextColor(context.getResources().getColor(i == index?txtSelectedColor:txtColor));
            imageViews.get(i).setSelected(i == index);
            imageViews.get(i).setImageResource(i == index?imgsSelected[i]:imgs[i]);
        }
    }
    /**
     * 设置圆点
     *
     * @param index 圆点索引
     * @param isMessage 是否有新消息，如果isMessage为true,显示小红点，为false才显示数目
     * @param count 数目
     */
    public void setDotsCount(int index, Boolean isMessage,int count) {
        if (imageViews == null || index > imageViews.size() - 1)
            return;
        if (count >= 0) {
            imageViews.get(index).setHasMessage(isMessage,count);
        }

    }

    public interface OnItemOnclickListener {
        void onItemClick(int index);
    }

    //px转dp
    private int dip2px(Context context, float dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }
}